﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using ComputerVision.Data;
using ComputerVision.Data.Repositories;

namespace ComputerVision.Services
{
    public class CapPheDuyetService:IService<CapPheDuyet>
    {
        private readonly IEntityRepository<CapPheDuyet> _capPheDuyetRepository;
        public CapPheDuyetService(IEntityRepository<CapPheDuyet> capPheDuyetRepository)
        {
            _capPheDuyetRepository = capPheDuyetRepository;
        }

        public CapPheDuyet Create(CapPheDuyet item)
        {
            try
            {
                if (item == null)
                    throw new ArgumentNullException("entity");
                _capPheDuyetRepository.InsertOnCommit(item);
                _capPheDuyetRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return item;
        }

        public CapPheDuyet Update(CapPheDuyet i)
        {
            CapPheDuyet item;
            try
            {
                item = _capPheDuyetRepository.Get(i.Id);
                item.Ten = i.Ten;
                _capPheDuyetRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine +
                               string.Format("Property: {0} Error: {1}", validationError.PropertyName,
                                   validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return item;
        }

        public CapPheDuyet Detail(Guid id)
        {
            var item = _capPheDuyetRepository.Get(id);
            return item;
        }

        public List<CapPheDuyet> ToList()
        {
            var item = _capPheDuyetRepository.GetAll().ToList();
            return item;
        }

        public void Delete(CapPheDuyet item)
        {
            try
            {
                if (item == null)
                    throw new ArgumentNullException("entity");
                _capPheDuyetRepository.DeleteOnCommit(item);
                _capPheDuyetRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;

                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine + string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
        }
    }
}
